package dataProviders;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import wrappers.Entity;
import wrappers.Link;

public class LinkComboBox implements ComboBoxModel{
	Set<ListDataListener> ldl = new HashSet<ListDataListener>();
	ArrayList<Entity> entities = new ArrayList<Entity>();
	ArrayList<Link> links = new ArrayList<Link>();
	String[] multType;
	Object selected = null;
	int selectedLink = 0;
	HashMap<String, Link> mapped = new HashMap<String, Link>();
	String[] linknames;
	
	public void setMultType(String[] type){
		multType = type;
	}
	
	public void setData(ArrayList<Entity> inputEntity, ArrayList<Link> inputLink){
		entities = inputEntity;
		links = inputLink;
		Entity[] arrayLink = new Entity[inputEntity.size()];
		for (int i = 0; i < arrayLink.length; i++) {
			arrayLink[i] = inputEntity.get(i);
		}
		for (Link link : inputLink) {
			String first = "";
			String second = "";
			for (int i = 0; i < arrayLink.length; i++) {
				if (link.getSource() == arrayLink[i].getId()) {
					first = arrayLink[i].getName();
				}
				if (link.getTarget() == arrayLink[i].getId()) {
					second = arrayLink[i].getName();
				}
			}
			mapped.put(first+"~"+second, link);
		}
		linknames = new String[mapped.size()];
		for (int i = 0; i < linknames.length; i++) {
			linknames[i] = (String) mapped.keySet().toArray()[i];
		}
	}
	
	@Override
	public void addListDataListener(ListDataListener arg0) {
		ldl.add(arg0);
		
	}

	@Override
	public Object getElementAt(int arg0) {
		
		return linknames[arg0];
	}

	@Override
	public int getSize() {
		return linknames.length;
	}

	@Override
	public void removeListDataListener(ListDataListener arg0) {
		ldl.remove(arg0);		
	}

	@Override
	public Object getSelectedItem() {
		return selected;
	}

	@Override
	public void setSelectedItem(Object arg0) {
		selected = arg0;
		System.out.println(arg0);
		
	}
	
	public Link getSelectedLink(){
		return mapped.get(selected);
	}
	
	public int getSelectedId(){
		return getSelectedLink().getId();
	}
	
}
